package com.panshi.modules.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import com.panshi.modules.domain.PageUtil;
import com.panshi.modules.domain.SysMenu;
import com.panshi.modules.service.SysMenuService;
import com.panshi.vo.R;
import com.panshi.vo.SelectVO;
import com.panshi.vo.SysMenuVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  前端控制器 :系统菜单
 * </p>
 *
 * @author 857
 * @since 2020-07-29
 */
@Slf4j
@CrossOrigin
@Api(tags = "系统菜单Controller")
@RestController
@RequestMapping("/api/sys/menu")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;


    @GetMapping("/list")
    @ApiOperation("查询菜单集合")
    public List<SysMenuVO> listSysMenu(){
        List<SysMenuVO> sysMenus = sysMenuService.listSysMenu();
        log.info("查询系统菜单: {}",sysMenus);
        return sysMenus;
    }

    @ApiOperation("分页查询菜单集合")
    @GetMapping("/pageMenuList/{current}/{limit}")
    public R pageMenuList(@PathVariable Integer current, @PathVariable Integer limit){
        Page<SysMenu> page = new Page<>(current,limit);
        IPage<SysMenu> sysMenuIPage = sysMenuService.pageMenuList(page);
        return R.ok().data("rows",sysMenuIPage.getRecords()).data("total",sysMenuIPage.getTotal());
    }

    @GetMapping("/listTree")
    @ApiOperation("树形菜单集合")
    public List<SysMenuVO> listSysMenuTree(){
        return sysMenuService.listSysMenuTree();
    }

    @GetMapping("/{id}")
    @ApiOperation("通过ID获取菜单信息")
    public SysMenuVO getSysMenu(@PathVariable("id") Integer id) {
        SysMenuVO sysMenu = sysMenuService.getSysMenu(id);
        return sysMenu;
    }
    @ApiOperation("查询所有菜单列表")
    @GetMapping(value = "/xmSelect",produces = {"application/json;charset=UTF-8"})
    public R xmSelect(){
        List<SelectVO> menuSelects = sysMenuService.xmSelect();
        return R.ok().data("data",menuSelects);
        //return menuSelects;
    }

    @ApiOperation("通过角色ID查询对应的菜单列表")
    @GetMapping(value = "/getRoleXmSelect/{id}",produces = {"application/json;charset=UTF-8"})
    public R getRoleXmSelect(@PathVariable("id") Integer id){
        List<SelectVO> roleXmselect = sysMenuService.getRoleXmselect(id);
        return R.ok().data("data",roleXmselect);
    }



    //==============================================================================================================

    @ApiOperation("树形表格数据")
    //@RequiresPermissions("menu:list:*")
    @GetMapping(value = "/treeTableMenu",produces = {"application/json;charset=UTF-8"})
    public Map<String,Object> treeTableMenu(){
        List<SysMenu> sysMenus = sysMenuService.listMenu();
        int count = sysMenuService.countMenus();
        /*PageUtil<List<SysMenu>> pageUtil = new PageUtil<>();
        pageUtil.setCode(0);
        pageUtil.setCount(count);
        pageUtil.setData(sysMenus);
        pageUtil.setMsg("");*/
        Map<String,Object> map = Maps.newHashMap();
        map.put("code",0);
        map.put("msg","success");
        map.put("count",count);
        map.put("data",sysMenus);
        return map;
    }

    @PostMapping
    @ApiOperation("新增菜单")
    ///@RequiresPermissions("menu:add:*")
    public R addSysMenu(@RequestBody @ApiParam(value = "SysMenuVO",name = "新增菜单参数") SysMenuVO sysMenuVO) {
        if(StringUtils.isNotBlank(sysMenuVO.getMenuname())){
            if(sysMenuService.countMenuname(sysMenuVO.getMenuname(),0) > 0){
                return R.error().msg("菜单名已存在");
            }
        }else {
            return R.error().msg("菜单名称不能为空");
        }

        if(StringUtils.isNotBlank(sysMenuVO.getPermission())){
            if(sysMenuService.countPermission(sysMenuVO.getPermission(),0) > 0){
                return R.error().msg("权限标识已存在");
            }
        }

        if(Objects.isNull(sysMenuVO.getPid())){
            sysMenuVO.setPid(-1);
        }
        int result = sysMenuService.addSysMenu(sysMenuVO);
        if(result > 0){
            return R.ok().msg("success");
        }
        return R.error().msg("failure");
    }

    @PutMapping
    @ApiOperation("编辑菜单")
    //@RequiresPermissions("menu:edit:*")
    public R editSysMenu(@RequestBody @ApiParam(name = "SysMenuVO" ,value ="编辑菜单接口") SysMenuVO sysMenuVO) {
        if(StringUtils.isNotBlank(sysMenuVO.getMenuname())){
            if(sysMenuService.countMenuname(sysMenuVO.getMenuname(),sysMenuVO.getId()) > 0){
                return R.error().msg("菜单名已存在");
            }
        }

        if(StringUtils.isNotBlank(sysMenuVO.getPermission())){
            if(sysMenuService.countPermission(sysMenuVO.getPermission(),sysMenuVO.getId()) > 0){
                return R.error().msg("权限标识已存在");
            }
        }
        int result = sysMenuService.editSysMenu(sysMenuVO);
        if(result > 0){
            return R.ok().msg("success");
        }
        return R.error().msg("failure");
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除菜单接口")
   // @RequiresPermissions("menu:del:*")
    public R delSysMenu(@PathVariable("id") Integer id) {
        int result = sysMenuService.delSysMenu(id);
        log.info("delSysMenu--{}",id);
        if(result > 0){
            return R.ok().msg("success");
        }
        return R.error().msg("failure");
    }
}

